import PIL.Image
from cStringIO import StringIO

class Generator(object):

    size = (128, 128)

    def thumbnail(self, f, size = None):
        """
        Return a thumbnail JPG as a string, given an image
        as a file object.
        """
        if size is None:
            size = self.size
        i = PIL.Image.open(f)
        i = i.convert('RGB')
        i.thumbnail(size, PIL.Image.ANTIALIAS)
        f = StringIO()
        i.save(f, 'JPEG')
        f.seek(0)
        return f.read()
